/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;

/**
 *
 * @author Alberto
 */



public class Datos {

    public static Alumno getAlumno(DataSource ds, String correouniversidad, String contrasena) {
        Alumno a=null;
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rset=null;
        try {
            conn = ds.getConnection();
            String consulta="Select * from alumno where correouniversidad=? and contrasena=?";
            pstmt=conn.prepareStatement(consulta);
            pstmt.setString(1, correouniversidad);
            pstmt.setString(2, contrasena);
            rset=pstmt.executeQuery();
            if(rset.next()) {
                System.out.println("");
                String  contactopariente= String.valueOf(rset.getString("contactopariente"));
                String nombre = String.valueOf(rset.getString("nombre"));
                String apellidos = String.valueOf(rset.getString("apellidos"));
                String telefono = String.valueOf(rset.getString("telefono"));
                int idalumno = Integer.parseInt(rset.getString("idalumno"));
                int idconsejerocurricular = Integer.parseInt(rset.getString("idconsejerocurricular"));

                System.out.println("El idalumno es "+idalumno);
                a=new Alumno(idconsejerocurricular, nombre, apellidos, telefono, contactopariente, correouniversidad, contrasena);
            }

        } catch (Exception ex) {
            Logger.getLogger(Datos.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            try {
                if(rset != null) rset.close();
                if(pstmt != null) pstmt.close();
                if(conn != null) conn.close();
            } catch (SQLException ex) {
                Logger.getLogger("Datos-->").log(Level.WARNING, "ERROR: {0}", ex.getLocalizedMessage());
            }
        }
        return a;
    }
//datos con los que estoy trabajando
    public static String getAlojamientoActual(DataSource ds, int idalumno) {
        ArrayList list=new ArrayList();
        String direccion=new String();
        ArrayList listHab=new ArrayList();
        Connection conn=null, conn2=null;
        Statement stmt=null, stmt2=null;
        ResultSet rset=null, rset2=null;
        try {
            conn = ds.getConnection();
            conn2 = ds.getConnection();
            String consulta = "Select * from contratoalumno where estado='vigente' and idalumno="+idalumno+";";
            stmt = conn.createStatement();
            stmt2 = conn2.createStatement();
            rset = stmt.executeQuery(consulta);
            //while(rset.next()){
                int idhabitacion = Integer.parseInt(rset.getString("idhabitacion"));
                //list.add(idhabitacion);
                String consulta2 = "Select * from habitacion where idhabitacion="+idhabitacion+";";
                rset2 = stmt2.executeQuery(consulta2);
                int idalojamiento=Integer.parseInt(rset2.getString("idalojamiento"));
                consulta2 = "Select * from alojamiento where idalojamiento="+idalojamiento+";";
                rset2=stmt2.executeQuery(consulta2);
                direccion = rset2.getString("direccion");
                
            //}
        } catch (Exception ex) {
            Logger.getLogger(Datos.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            try {
                if(rset != null) rset.close();
                if(stmt != null) stmt.close();
                if(conn != null) conn.close();
                if(rset2 != null) rset2.close();
                if(stmt2 != null) stmt2.close();
                if(conn2 != null) conn2.close();
            } catch (SQLException ex) {
                Logger.getLogger("Datos-->").log(Level.WARNING, "ERROR: {0}", ex.getLocalizedMessage());
            }
        }

        return direccion;
        //return list;
    }

}